<?php

class PrivilegeInfoDao extends Dao {

    function __construct(array $attributes = array()) {
        parent::__construct($attributes);
        $this->table = 'privilegeinfo';
    }
    
    public function insertObject($obj){
            
            $result = DB::insert(DB::raw("INSERT INTO privilegeinfo (group_id, module_id, functions_id, is_allow_access) ".
                    "VALUES ('".$obj->getGroup()->getId()."' ".
                            ", '".$obj->getModule()->getId()."' ".
                            ", '".$obj->getFunctions()->getId()."' ".
                            ", '".$obj->getIsAllowAccess()."' ".
                            ")"));
            if (!$result) { return false; }
            return DB::getPdo()->lastInsertId();
    }

    public function updateObject($obj, $old_id){

        DB::update(DB::raw("UPDATE privilegeinfo SET group_id='".$obj->getGroup()->getId()."'".
                ", module_id='".$obj->getModule()->getId()."'".
                ", functions_id='".$obj->getFunctions()->getId()."'".
                ", is_allow_access='".$obj->getIsAllowAccess()."'".
                " WHERE id=".$old_id." "));
    return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM privilegeinfo where id='".$id."'"));

        return true;
    }
    
    public function getObject($id){
        
        $result = DB::select(DB::raw("SELECT * FROM PrivilegeInfo where id='".$id."'"));
        return $result;
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM PrivilegeInfo"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    public function getListByGroupId($groupId){
        try {
            $queryResult = DB::select( DB::raw("SELECT PrivilegeInfo.id as privilege_id".
                "     , PrivilegeInfo.is_allow_access".
                "     , Module.id as module_id".
                "     , Module.name as module_name".
                "     , Module.sequence as module_sequence".
                "     , Module.icon as module_icon".
                "     , Module.is_enabled as module_is_enabled".
                "     , Functions.id as functions_id".
                "     , Functions.name as functions_name".
                "     , Functions.icon as functions_icon".
                "     , Functions.is_enabled as functions_is_enabled".
                "     , Functions.url".
                "     , Functions.sequence as functions_sequence".
                "     , Functions.is_show".
                "     , UserGroup.id as group_id".
                "     , UserGroup.name".
                "     , UserGroup.is_enabled as group_is_enabled".
                " FROM PrivilegeInfo ".
                "     INNER JOIN Functions ON PrivilegeInfo.functions_id = Functions.id".
                "     INNER JOIN Module ON Functions.module_id = Module.id".
                "     INNER JOIN UserGroup ON PrivilegeInfo.group_id = UserGroup.id".
                " WHERE UserGroup.id = '" . $groupId . "'".
                " ORDER BY Module.sequence, Functions.sequence") );
            //$queryResult = parent::where('group_id','=',$groupId)->get();
            $results = array();
            foreach($queryResult as $obj){
                $privilegeInfo = new PrivilegeInfo();
                $privilegeInfo->setId($obj->privilege_id);
                    $group = new UserGroup();
                    $group->setId($obj->group_id);
                    $group->setName($obj->name);
                    $group->setIsEnabled($obj->group_is_enabled);
                    $group->IsLoaded(true);
                $privilegeInfo->setGroup($group);
                    $module = new Module();
                    $module->setId($obj->module_id);
                    $module->setName($obj->module_name);
                    $module->setSequence($obj->module_sequence);
                    $module->setIcon($obj->module_icon);
                    $module->setIsEnabled($obj->module_is_enabled);
                    $module->setIsLoaded(true);
                $privilegeInfo->setModule($module);
                    $function = new Functions();
                    $function->setId($obj->functions_id);
                    $function->setName($obj->functions_name);
                    $function->setUrl($obj->url);
                    $function->setIcon($obj->functions_icon);
                    $function->setSequence($obj->functions_sequence);
                    $function->setIsShow($obj->is_show);
                    $function->setIsEnabled($obj->functions_is_enabled);
                    $function->setIsLoaded(true);
                $privilegeInfo->setFunctions($function);
                $privilegeInfo->setIsAllowAccess($obj->is_allow_access);
                $privilegeInfo->setIsLoaded(true);
                $results[] = $privilegeInfo;
            }

            return $results;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
    function updateGroupId($groupId,$oldGroupId){
         try {
            DB::select( DB::raw("update PrivilegeInfo set group_id = '$groupId' where group_id = '$oldGroupId' ") );
            return true;
         } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
    public function toObject($rowdata=null){
        $privilegeInfo = new PrivilegeInfo();
        $privilegeInfo->setId($rowdata->id);
        $group = new UserGroup();
        $group->setId($rowdata->group_id);
        $group->IsLoaded(false);
        $privilegeInfo->setGroup($group);
        $module = new Module();
        $module->setId($rowdata->module_id);
        $module->setIsLoaded(false);
        $privilegeInfo->setModule($module);
        $function = new Functions();
        $function->setId($rowdata->functions_id);
        $function->setIsLoaded(false);
        $privilegeInfo->setFunctions($function);
        $privilegeInfo->setIsAllowAccess($rowdata->is_allow_access);
        $privilegeInfo->setIsLoaded(true);
        
        return $privilegeInfo;
    }
    
}
